<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"  http-equiv="content-type" />
  <link rel="stylesheet" type="text/css" href="../style.css" />
  <title>SOCI - SQLite3 Backend Reference</title>
</head>

<body>
<p class="banner">SOCI - The C++ Database Access Library</p>

<h2>SQLite3 Backend Reference</h2>

<div class="navigation">
<a href="#prerequisites">Prerequisites</a><br />
<div class="navigation-indented">
  <a href="#versions">Supported Versions</a><br />
  <a href="#tested">Tested Platforms</a><br />
  <a href="#required">Required Client Libraries</a><br />
</div>
<a href="#connecting">Connecting to the Database</a><br />
     
<a href="#support">SOCI Feature Support</a><br />
<div class="navigation-indented">
    <a href="#dynamic">Dynamic Binding</a><br />
    <a href="#bindingbyname">Binding by Name</a><br />
    <a href="#bulk">Bulk Operations</a><br />
    <a href="#transactions">Transactions</a><br />
    <a href="#blob">BLOB Data Type</a><br />
    <a href="#rowid">RowID Data Type</a><br />
    <a href="#nested">Nested Statements</a><br />
    <a href="#procedures">Stored Procedures</a><br />
</div>
  <a href="#native">Accessing the Native Database API</a><br />
  <a href="#extensions">Backend-specific Extensions</a><br />
  <a href="#options">Configuration options</a><br />
</div>

<h3 id="prerequisites">Prerequisites</h3>
<h4 id="versions">Supported Versions</h4>

<p>The SOCI SQLite3 backend is supported for use with SQLite3 >= 3.1</p>

<h4 id="tested">Tested Platforms</h4>

<table border="1" cellpadding="5" cellspacing="0">
<tbody>
<tr><th>SQLite3 version</th><th>Operating System</th><th>Compiler</th></tr>
<tr><td>3.5.2</td><td>Mac OS X 10.5</td><td>g++ 4.0.1</td></tr>
<tr><td>3.1.3</td><td>Mac OS X 10.4</td><td>g++ 4.0.1</td></tr>
<tr><td>3.2.1</td><td>Linux i686 2.6.10-gentoo-r6</td><td>g++ 3.4.5</td></tr>
<tr><td>3.3.4</td><td>Ubuntu 5.1</td><td>g++ 4.0.2</td></tr>
<tr><td>3.3.4</td><td>Windows XP</td><td>(cygwin) g++ 3.3.4</td></tr>
<tr><td>3.3.4</td><td>Windows XP</td><td>Visual C++ 2005 Express Edition</td></tr>
<tr><td>3.3.8</td><td>Windows XP</td><td>Visual C++ 2005 Professional</td></tr>
<tr><td>3.4.0</td><td>Windows XP</td><td>(cygwin) g++ 3.4.4</td></tr>
<tr><td>3.4.0</td><td>Windows XP</td><td>Visual C++ 2005 Express Edition</td></tr>
</tbody>
</table>

<h4 id="required">Required Client Libraries</h4>

<p>The SOCI SQLite3 backend requires SQLite3's <code>libsqlite3</code> client library.</p>

<h4 id="connecting">Connecting to the Database</h4>

<p>To establish a connection to the SQLite3 database, create a Session object
using the <code>SQLite3</code> backend factory together with the database file name:</p>

<pre class="example">
session sql(sqlite3, "database_filename");
</pre>

<p>The only option for the connection string is the name of the file to use as a database.</p>

<p>Once you have created a <code>session</code> object as shown above, you can use it to access the database, for example:</p>
<pre class="example">
int count;
sql &lt;&lt; "select count(*) from invoices", into(count);
</pre>

<p>(See the <a href="../basics.html">SOCI basics</a> and <a href="../exchange.html">exchanging data</a> documentation for general information on using the <code>session</code> class.)</p>

<h3 id="support">SOCI Feature Support</h3>
<h4 id="dynamic">Dynamic Binding</h4>

<p>The SQLite3 backend supports the use of the SOCI <code>row</code> class, which facilitates retrieval of data whose type is not known at compile time.</p>

<p>When calling <code>row::get&lt;T&gt;()</code>, the type you should pass as T depends upon the underlying database type.</p>  

<p>For the SQLite3 backend, this type mapping is complicated by the fact the SQLite3 does not enforce types <a href="#INTEGER_PRIMARY_KEY">*</a>, and makes no attempt to validate the type names used in table creation or alteration statements.    SQLite3 will return the type as a string, SOCI will recognize the following strings and match them the corresponding SOCI types:</p>

<table border="1" cellpadding="5" cellspacing="0">
  <tbody>
    <tr>
      <th>SQLite3 Data Type</th>
      <th>SOCI Data Type</th>
      <th><code>row::get&lt;T&gt;</code> specializations</th>
    </tr>
    <tr>
      <td>*float*</td>
      <td><code>dt_ouble</code></td>
      <td><code>double</code></td>
    </tr>
    <tr>
      <td>*int*</td>
      <td><code>dt_integer</code></td>
      <td><code>int</code></td>
    </tr>
    <tr>
      <td>*char*</td>
      <td><code>dt_string</code></td>
      <td><code>std::string</code></td>
    </tr>
    <tr>
      <td>*date*, *time*</td>
      <td><code>dt_date</code></td>
      <td><code>std::tm</code><code></code></td>
    </tr>
  </tbody>
</table>

<p id="INTEGER_PRIMARY_KEY">* There is one case where SQLite3 enforces type.  If a column is declared as "integer primary key", then SQLite3 uses that as an alias to the internal ROWID column that exists for every table.  Only integers are allowed in this column.</p>

<p>(See the <a href="../exchange.html#dynamic">dynamic resultset binding</a> documentation for general information on using the <code>row</code> class.)</p>

<h4 id="bindingbyname">Binding by Name</h4>

<p>In addition to <a href="../exchange.html#bind_position">binding by position</a>, the SQLite3 backend supports <a href="../exchange.html#bind_name">binding by name</a>, via an overload of the <code>use()</code> function:</p>

<pre class="example">
int id = 7;
sql &lt;&lt; "select name from person where id = :id", use(id, "id")
</pre>

<p>The backend also supports the SQLite3 native numbered syntax, "one or more literals can be replace by a parameter "?" or ":AAA" or "@AAA" or "$VVV" where AAA is an alphanumeric identifier and VVV is a variable name according to the syntax rules of the TCL programming language." <a href="http://www.sqlite.org/capi3ref.html#sqlite3_bind_int">[1]</a>:</p>

<pre class="example">
int i = 7;
int j = 8;
sql &lt;&lt; "insert into t(x, y) values(?, ?)", use(i), use(j);
</pre>

<h4 id="bulk">Bulk Operations</h4>

<p>The SQLite3 backend has full support for SOCI's <a href="../statements.html#bulk">bulk operations</a> interface.  However, this support is emulated and is not native.</p>

<h4 id="transactions">Transactions</h4>

<p><a href="../statements.html#transactions">Transactions</a> are also fully supported by the SQLite3 backend.</p>

<h4 id="blob">BLOB Data Type</h4>

<p>The SQLite3 backend supports working with data stored in columns of type Blob, via SOCI's blob class.  Because of SQLite3 general typelessness the column does not have to be declared any particular type.</p>
<h4 id="rowid">RowID Data Type</h4>

<p>In SQLite3 RowID is an integer.  "Each entry in an SQLite table has a unique integer key called the "rowid". The rowid is always available as an undeclared column named ROWID, OID, or _ROWID_. If the table has a column of type INTEGER PRIMARY KEY then that column is another an alias for the rowid."<a href="http://www.sqlite.org/capi3ref.html#sqlite3_last_insert_rowid">[2]</a></p>

<h4 id="nested">Nested Statements</h4>

<p>Nested statements are not supported by SQLite3 backend.</p>

<h4 id="procedures">Stored Procedures</h4>

<p>Stored procedures are not supported by SQLite3 backend</p>

<h3 id="native">Acessing the native database API</h3>

<p>SOCI provides access to underlying datbabase APIs via several 
<code>get_backend()</code> functions, as described in the 
<a href="../beyond.html">beyond SOCI</a> documentation.</p>

<p>The SQLite3 backend provides the following concrete classes for navite API access:</p>

<table border="1" cellpadding="5" cellspacing="0">
  <tbody>
    <tr>
      <th>Accessor Function</th>
      <th>Concrete Class</th>
    </tr>
    <tr>
      <td><code>session_backend* session::get_backend()</code></td>
      <td><code>sqlie3_session_backend</code></td>
    </tr>
    <tr>
      <td><code>statement_backend* statement::get_backend()</code></td>
      <td><code>sqlite3_statement_backend</code></td>
    </tr>
    <tr>
      <td><code>rowid_backend* rowid::get_backend()</code></td>
      <td><code>sqlite3_rowid_backend</code></td>
    </tr>
  </tbody>
</table>


<h3 id="extensions">Backend-specific extensions</h3>

<p>None.</p>

<h3 id="options">Configuration options</h3>

<p>None</p>

<p class="copyright">Copyright &copy; 2004-2006 Maciej Sobczak, Stephen Hutton, David Courtney</p>
</body>
</html>
